﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace FileTesterFormat
{
    public partial class MainForm : Form
    {
        private Rules allRules;
        private string fileContent = string.Empty;
        private string[] fileLines;

        private int currentLineIndex = 0;

        public MainForm()
        {
            InitializeComponent();

            this.Load += (oo, ee) =>
            {
                BuildStaticList();
                ReadRules();
            };

            comboRules.SelectedValueChanged += (oo, ee) =>
            {

            };

            btnLoadFile.Click += (oo, ee) =>
            {
                LoadFile();
            };

            btnNextLine.Click += (oo, ee) =>
            {
                if (fileLines != null && (currentLineIndex + 1) < fileLines.Count())
                {
                    currentLineIndex++;
                    StartParsing();
                }
            };

            btnPrevious.Click += (oo, ee) =>
            {
                if (fileLines != null && currentLineIndex > 0)
                {
                    currentLineIndex--;
                    StartParsing();
                }
            };
        }

        private void LoadFile()
        {
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (var fileReader = new StreamReader(openFileDialog.OpenFile()))
                {
                    fileContent = fileReader.ReadToEnd();
                    fileLines = fileContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                }

                richTextBox1.Text = fileContent;
                StartParsing();
            }
        }

        private void StartParsing()
        {
            if (fileLines.Count() >= currentLineIndex + 1)
            {
                PopulateRulesFields(fileLines.ElementAt(currentLineIndex));
            }
        }

        private void ReadRules()
        {
            var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Rules.xml");
            using (var filestream = new StreamReader(filePath))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(Rules));
                allRules = (Rules)deserializer.Deserialize(filestream);
            }

            BindComboRules();
            statusLabel.Text = "All rules are loaded !";
        }

        private void PopulateRulesFields(string line)
        {
            var selectedRule = GetSelectedRule();
            panel.Controls.Clear();

            foreach (var ruleInfo in selectedRule.RuleItems)
            {
                var usercontrol = new UserControls.RuleFieldControl();
                usercontrol.SetText(line, ruleInfo.Description, ruleInfo.StartIndex, ruleInfo.StopIndex);

                panel.Controls.Add(usercontrol);
            }
        }

        private void BindComboRules()
        {
            comboRules.DataSource = allRules;
            comboRules.DisplayMember = "Title";

            PopulateRulesFields("");
        }

        private RulesDescription GetSelectedRule()
        {
            return (RulesDescription)comboRules.SelectedValue;
        }

        private void BuildStaticList()
        {
            RulesStatic.Create();
            statusLabel.Text = "Rules catalog is created !";
        }
    }
}
